from django.shortcuts import render
from django.http import HttpResponse
from . models import *
import os,time

###Dashboard订单视图
#获取订单列表页面
def order_list_page(request,p_index = 1):
	orders = Orders.objects.filter()
	for v in orders:
		v.username = Users.objects.get(id = v.uid).username

	#多条件筛选判断
	if request.GET.get('search_type') == 'oid':
		orders = Orders.objects.filter(id__contains = request.GET.get('keywords'))
	elif request.GET.get('search_type') == 'ostatus':
		if request.GET.get('keywords') == '未支付':
			orders = Orders.objects.filter(status = 0)
		elif request.GET.get('keywords') == '已发货':
			orders = Orders.objects.filter(status = 1)
		elif request.GET.get('keywords') == '未收货':
			orders = Orders.objects.filter(status = 2)
		elif request.GET.get('keywords') == '已收货':
			orders = Orders.objects.filter(status = 3)
		elif request.GET.get('keywords') == '已取消':
			orders = Orders.objects.filter(status = 4)				

	#获取分页数据
	from django.core.paginator import Paginator
	p_index = int(p_index)
	#进行数据分页，实例化对象，设置每页返回数据的条数
	p = Paginator(orders,5)
	#获取分页后的数据
	orders = p.page(p_index)

	re_info = {'orders_info':orders}

	return render(request,'dash/order/list_page.html',re_info)

#获取订单详情页面
def order_detail_page(request):
	#根据订单id获取订单详情
	items = Items.objects.filter(orderid = request.GET['oid'])

	re_info = {'items_info':items}

	return render(request,'dash/order/detail_page.html',re_info)

#获取订单编辑页面
def order_edit_page(request):
	#根据订单id获取订单信息
	order_info = Orders.objects.get(id = request.GET['oid'])
	#封装返回数据
	re_info = {'order_info':order_info}

	return render(request,'dash/order/edit_page.html',re_info)

#执行编辑订单
def edit_order(request):
	#根据订单id获取订单对象
	order = Orders.objects.get(id = request.POST['oid'])
	order.linkman = request.POST['linkman']
	order.address = request.POST['address']
	order.code = request.POST['code']
	order.phone = request.POST['phone']
	order.status = request.POST['status']
	#保存对象属性
	order.save()
	#修改成功，跳转至订单列表页面
	return HttpResponse('<script>alert("修改成功");location.href="/dashboard/order/order_list_page"</script>')












		



	













